package cn.yunhe.comunicate;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;

public class ChatClient extends JFrame {
	private static final long serialVersionUID = 1L;
	private Socket socket; // 负责和服务器通信
	private JTextArea sendArea; // 消息编辑区域
	private JTextArea contentArea; // 群聊消息显示框
	private String name; // 当前用户名称
	PrintWriter out = null;

	public ChatClient(Socket socket, String name) {
		this.socket = socket;
		this.name = name;
		this.init(); // 初始化聊天客户端
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);

		/*
		 * 接下来启动单独线程，专门从服务器中读取数据
		 * 
		 */

		ClientThread thread = new ClientThread(socket, contentArea);
		thread.start();
	}

	public void init() {
		this.setTitle("我的聊天室");
		this.setSize(300, 400);
		int x = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int y = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();

		this.setLocation((x - this.getWidth()) / 2, (y - this.getHeight()) / 2);
		this.setResizable(false); // 不允许用户改变大小

		contentArea = new JTextArea();// 显示区
		// contentArea.setEnabled(false); //禁止编辑
		contentArea.setLineWrap(true); // 换行方法
		JScrollPane logPanel = new JScrollPane(contentArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		sendArea = new JTextArea(); // 输入区
		sendArea.setLineWrap(true); // 控制每行显示长度最大不超过界面长度
		JScrollPane sendPanel = new JScrollPane(sendArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		// 创建一个分隔窗格
		JSplitPane splitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, logPanel, sendPanel);
		splitpane.setDividerLocation(250);
		this.add(splitpane, BorderLayout.CENTER);

		// 按钮面板

		JPanel bPanel = new JPanel();
		bPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.add(bPanel, BorderLayout.SOUTH);

		JLabel namelabel = new JLabel("昵称： " + this.name + "  ");
		bPanel.add(namelabel);

		JButton closeButton = new JButton("关闭");
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(out != null){
					out.close();
				}
				System.exit(0);
			}
		});
		bPanel.add(closeButton);

		JButton sendButton = new JButton("发送");

		sendButton.addActionListener(new ActionListener() {
			// @Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String str = sendArea.getText();
				SimpleDateFormat formater = new SimpleDateFormat("HH:mm:ss");
				String time = formater.format(new Date());
				String sendStr = name + " " + time + " 说: \n" + str;

				try {
					out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
					out.println(sendStr);
					out.flush();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				sendArea.setText("");
			}
		});

		bPanel.add(sendButton);
	}

}

// 客户端与服务器端通信的线程类
class ClientThread extends Thread {
	private Socket socket;
	private JTextArea contentArea;

	public ClientThread(Socket socket, JTextArea conteArea) {
		this.socket = socket;
		this.contentArea = conteArea;
	}

	public void run() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String str = br.readLine();
			while (str != null) {
					System.out.println(str);
					contentArea.append(str);
					contentArea.append("\n");
					str = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}